// *************************************************************************************************
//
// MK3D Engine
// --------------------------------------
// Copyright (C) 2007-2011 Zeb (mailto: zebbey@gmail.com)
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// *************************************************************************************************

#include "mk_header.h"
#include "mk_log.h"

namespace MK3DE
{
	CLog::CLog() : _startTime(0)
	{
	}

	CLog::~CLog()
	{
		_logFile << "</table>\n";
		_logFile << "</div>\n";

		_logFile.close();
	}

	void CLog::Create(const char* logFileName, const char* name, const char* version)
	{
		_startTime = timeGetTime();

		// Reset log file
		_logFile.open(logFileName, std::ios::out);

		_logFile << "<style type=\"text/css\">\n";

		_logFile << "body, html {\n";
		_logFile << "background: #002000;\n";
		_logFile << "width: 1000px;\n";
		_logFile << "font-family: Verdana;\n";
		_logFile << "font-size: 12px;\n";
		_logFile << "color: #C0C0C0;\n";
		_logFile << "}\n";

		_logFile << "h1 {\n";
		_logFile << "color : #FFFFFF;\n";
		_logFile << "border-bottom : 1px dotted #888888;\n";
		_logFile << "}\n";

		_logFile << ".box {\n";
		_logFile << "border : 1px dotted #818286;\n";
		_logFile << "padding : 5px;\n";
		_logFile << "margin: 5px;\n";
		_logFile << "width: 950px;\n";
		_logFile << "background-color : #292929;\n";
		_logFile << "}\n";

		_logFile << ".inf {\n";
		_logFile << "color: #C0C0C0;\n";
		_logFile << "font-family: Verdana;\n";
		_logFile << "}\n";

		_logFile << ".err {\n";
		_logFile << "color: #EE1100;\n";
		_logFile << "font-family: Verdana;\n";
		_logFile << "font-weight: bold\n";
		_logFile << "}\n";

		_logFile << ".war {\n";
		_logFile << "color: #FFCC00;\n";
		_logFile << "font-weight: bold\n";
		_logFile << "}\n";

		_logFile << "</style>\n\n";

		_logFile << "<h1>" << name << "</h1>\n";
		_logFile << "<h3>" << "Version: " << version << "</h3>\n";
		_logFile << "<div class=\"box\">\n";
		_logFile << "<table>\n";

		_logFile.flush();
	}

	void CLog::__WriteLine(const char* text, const char* typeName)
	{
		// Write to log file
		_logFile << "<tr>\n";

		_logFile << "<td width=\"100\">";
		_logFile << (timeGetTime() - _startTime) / 1000.0f;

		_logFile << "</td>\n";

		_logFile << "<td class=\"" << typeName << "\">";
		_logFile << text;
		_logFile << "</td>\n";

		_logFile << "</tr>\n";

		_logFile.flush();
	}

	void CLog::Write(MK_LOG_TYPE type, const char* text, ...)
	{
		if (!text || !text[0])
			return;

		char szLogBuffer[MAX_LOG_BUFFER];
		va_list list;
		va_start(list, text);
		wvsprintf(szLogBuffer, text, list);

		switch (type)
		{
		case MKLT_INFO:
			__WriteLine(szLogBuffer, TEXT("inf"));
			break;

		case MKLT_WARNING:
			__WriteLine(szLogBuffer, TEXT("war"));
			break;

		case MKLT_ERROR:
			__WriteLine(szLogBuffer, TEXT("err"));
			break;

		default:
			break;
		};

		va_end( list );
	}

}